package com.dream.tvsearcher.activity;


import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.dream.tvsearcher.R;
import com.dream.tvsearcher.bean.SearchBean;
import com.dream.tvsearcher.inter.ISearchInterface;
import com.dream.tvsearcher.utils.ApiManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends BaseActivity {
    Toolbar bar;
    boolean showtv=false;
    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {
        bar= (Toolbar) findViewById(R.id.bar);
    }

    @Override
    protected void setEvent() {

    }

    @Override
    protected void init() {
        bar.setSubtitle("请输入你想搜索的影视吧");
        bar.setTitle("");
        setSupportActionBar(bar);
    }

    @Override
    protected void loadData() {

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        MenuItem item = menu.findItem(R.id.search);
        SearchView actionView = (SearchView) item.getActionView();
        actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                showFragment(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    private void showFragment(String text) {
        showtv=true;
        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(ApiManager.BASE).build();
        ISearchInterface iSearchInterface=retrofit.create(ISearchInterface.class);
        Call<SearchBean> call=iSearchInterface.getBean(text);
        call.enqueue(new Callback<SearchBean>() {
            @Override
            public void onResponse(Call<SearchBean> call, Response<SearchBean> response) {
                SearchBean bean=response.body();
                Fragment fragment=new MyFragment();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fl,fragment);
                Bundle bundle=new Bundle();
                bundle.putSerializable(SearchBean.BEANKEY,bean);
                fragment.setArguments(bundle);
                transaction.commit();
            }

            @Override
            public void onFailure(Call<SearchBean> call, Throwable t) {
                Snackbar.make(bar,"对不起，请检查网络设置",Snackbar.LENGTH_SHORT).show();
            }
        });
    }
    private long clicktime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (showtv==true){
                showWX();
                return false;
            }else{
                exit();
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (System.currentTimeMillis()-clicktime>2000){
            Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
            clicktime=System.currentTimeMillis();
        }else{
            this.finish();
            System.exit(0);
        }
    }

    private void showWX() {
        WXFragment fragment=new WXFragment();
        FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl,fragment).commit();
        showtv=false;
    }
}
